﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.BaseContent;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace Job.Web
{
    public partial class Index : BasePage
    {
        BaseContentLogic contentLogic = new BaseContentLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                LoadAbout();
                BindNewsList();
                BindHunterJob();

                string ctrlPath = "~/userControls/Public/BottomNews.ascx";
                this.LoadCtrl(ctrlPath, "phBottomNews");

                //BindHRList();
                //BindManagerList();
                //BindIndustryList();
            }
        }

        private void LoadAbout()
        {
            JobBaseContent content = contentLogic.QuickGetContentBySubjectId((int)Constants.SubjectType.CompanyDescription);
            if (null != content)
            {
                this.lbAboutCompany.Text = Functions.HtmlEncode( content.Description );
            }
            content = contentLogic.QuickGetContentByContentId((int)Constants.SubjectType.HunterDescription);
            if (null != content)
            {
                this.lbAbountHunter.Text = Functions.HtmlEncode(content.Description);
            }
            content = contentLogic.QuickGetContentByContentId((int)Constants.SubjectType.ServiceDescription);
            if (null != content)
            {
                this.lbAboutService.Text = Functions.HtmlEncode(content.Description);
            }
        }

        //绑定左边的新闻列表
        private void BindNewsList()
        {
            const int RowCount = 7;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyFastList, RowCount);

            if (ds != null)
            {
                this.newsList.DataSource = ds.Tables[0];
                this.newsList.DataBind();

                if (ds.Tables[0].Rows.Count >= RowCount)
                {
                    this.moreNews.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyFastList).ToString();
                    this.moreNews.Visible = true;
                }
                else
                {
                    this.moreNews.Visible = false;
                }

            }
            else
            {
                this.moreNews.Visible = false;
            }
        }

        ////绑定HR频道列表
        //private void BindHRList()
        //{
        //    const int RowCount = 5;
        //    DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyHRList, RowCount);

        //    if (ds != null)
        //    {
        //        this.hrList.DataSource = ds.Tables[0];
        //        this.hrList.DataBind();


        //        if (ds.Tables[0].Rows.Count >= RowCount)
        //        {
        //            this.moreHR.NavigateUrl = WebCommon.GetApplicationPath(Request)
        //                + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
        //                + "&subjectid=" + ((int)Constants.SubjectType.CompanyHRList).ToString();
        //            this.moreHR.Visible = true;
        //        }
        //        else
        //        {
        //            this.moreHR.Visible = false;
        //        }
        //    }
        //    else
        //    {
        //        this.moreHR.Visible = false;
        //    }
        //}

        ////绑定行业动态列表
        //private void BindIndustryList()
        //{
        //    const int RowCount = 5;
        //    DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyIndustryList, RowCount);

        //    if (ds != null)
        //    {
        //        this.industryList.DataSource = ds.Tables[0];
        //        this.industryList.DataBind();

        //        if (ds.Tables[0].Rows.Count >= RowCount)
        //        {
        //            this.moreIndustry.NavigateUrl = WebCommon.GetApplicationPath(Request)
        //                + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
        //                + "&subjectid=" + ((int)Constants.SubjectType.CompanyIndustryList).ToString();                     

        //            this.moreIndustry.Visible = true;
        //        }
        //        else
        //        {
        //            this.moreIndustry.Visible = false;
        //        }
        //    }
        //    else
        //    {
        //        this.moreIndustry.Visible = false;
        //    }
        //}

        ////绑定管理资讯列表
        //private void BindManagerList()
        //{
        //    const int RowCount = 5;
        //    DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyManagerList, RowCount);

        //    if (ds != null)
        //    {
        //        this.managerList.DataSource = ds.Tables[0];
        //        this.managerList.DataBind();

        //        if (ds.Tables[0].Rows.Count >= RowCount)
        //        {
        //            this.moreManager.NavigateUrl = WebCommon.GetApplicationPath(Request)
        //                + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
        //                + "&subjectid=" + ((int)Constants.SubjectType.CompanyManagerList).ToString();
        //            this.moreManager.Visible = true;
        //        }
        //        else
        //        {
        //            this.moreManager.Visible = false;
        //        }
        //    }
        //    else
        //    {
        //        this.moreManager.Visible = false;
        //    }
        //}

        //绑定猎头职位列表
        private void BindHunterJob()
        {
            HunterJobLogic jobLogic = new HunterJobLogic();
            DataTable dt = jobLogic.GetTopJob(10);
            if (null != dt)
            {
                jobList.DataSource = dt;
                jobList.DataBind();
            }
        }


        protected void newsList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink hlDetail = (HyperLink)e.Item.FindControl("newsLink");
                        Label lbContentId = (Label)e.Item.FindControl("newsId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");
                        BaseInfo subjectInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        hlDetail.NavigateUrl = "Item.aspx?categoryid=" 
                            + Convert.ToString(subjectInfo.CatagoryId) + "&subjectid=" 
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (hlDetail.Text.Length > 13)
                            hlDetail.Text = hlDetail.Text.Substring(0, 13) + "...";

                        break;
                    }
            }
        }



        protected void jobList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink jobDetail = (HyperLink)e.Item.FindControl("jobLink");
                        Label lbJobNo = (Label)e.Item.FindControl("lbNo");
                        
                        BaseInfo info = WebCommon.GetSubjectInfo((int)Constants.SubjectType.HunterPositionList);
                        jobDetail.NavigateUrl = "Item.aspx?categoryid=" 
                            + Convert.ToString(info.CatagoryId) + "&subjectid=" 
                            + Convert.ToString((int)Constants.SubjectType.HunterPositionList)
                            + "&jobno=" + lbJobNo.Text.Trim();
                        break;
                    }
            }
        }


        //得到某个Subject所对应的前N条记录
        private DataSet QueryTopList(int subject, int number)
        {
            ArrayList param = new ArrayList();

            SqlParameter pm = new SqlParameter();
            pm.ParameterName = "subject_id";
            pm.Value = subject;
            pm.SqlDbType = SqlDbType.Int;
            param.Add(pm);

            DataSet ds = contentLogic.GetBaseContentList(number, 1, 0, "order_no", Job.Web.Framework.SortDirection.Descending, param);
            return ds;
        }

        //protected void hrList_ItemDataBound(object sender, DataListItemEventArgs e)
        //{
        //    ListItemType type = e.Item.ItemType;
        //    switch (type)
        //    {
        //        case ListItemType.Item:
        //        case ListItemType.AlternatingItem:
        //            {
        //                HyperLink hrDetail = (HyperLink)e.Item.FindControl("hrLink");
        //                Label lbContentId = (Label)e.Item.FindControl("hrId");
        //                Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

        //                BaseInfo hrInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

        //                hrDetail.NavigateUrl = "Item.aspx?categoryid="
        //                    + Convert.ToString(hrInfo.CatagoryId) + "&subjectid="
        //                    + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
        //                if (hrDetail.Text.Length > 15)
        //                    hrDetail.Text = hrDetail.Text.Substring(0, 15) + "...";
        //                break;
        //            }
        //    }
        //}

        //protected void managerList_ItemDataBound(object sender, DataListItemEventArgs e)
        //{
        //    ListItemType type = e.Item.ItemType;
        //    switch (type)
        //    {
        //        case ListItemType.Item:
        //        case ListItemType.AlternatingItem:
        //            {
        //                HyperLink managerDetail = (HyperLink)e.Item.FindControl("managerLink");
        //                Label lbContentId = (Label)e.Item.FindControl("managerId");
        //                Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

        //                BaseInfo managerInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

        //                managerDetail.NavigateUrl = "Item.aspx?categoryid="
        //                    + Convert.ToString(managerInfo.CatagoryId) + "&subjectid="
        //                    + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
        //                if (managerDetail.Text.Length > 15)
        //                    managerDetail.Text = managerDetail.Text.Substring(0, 15) + "...";
        //                break;
        //            }
        //    }
        //}

        //protected void industryList_ItemDataBound(object sender, DataListItemEventArgs e)
        //{
        //    ListItemType type = e.Item.ItemType;
        //    switch (type)
        //    {
        //        case ListItemType.Item:
        //        case ListItemType.AlternatingItem:
        //            {
        //                HyperLink industryDetail = (HyperLink)e.Item.FindControl("industryLink");
        //                Label lbContentId = (Label)e.Item.FindControl("industryId");
        //                Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

        //                BaseInfo industryInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

        //                industryDetail.NavigateUrl = "Item.aspx?categoryid="
        //                    + Convert.ToString(industryInfo.CatagoryId) + "&subjectid="
        //                    + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
        //                if (industryDetail.Text.Length > 15)
        //                    industryDetail.Text = industryDetail.Text.Substring(0, 15) + "...";
        //                break;
        //            }
        //    }

        //}

    }
}
